//
//  ACEDatePickerView.m
//  ACE
//
//  Created by zhangtao on 2018/8/14.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "ACEDatePickerView.h"

@implementation ACEDatePickerView

{
    UIDatePicker *datePicker;
    NSString *value;
}

-(instancetype)initWithFrame:(CGRect)frame withBackFormatter:(NSString *)foramtter{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        _formatter = foramtter;
        datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-50)];
        [datePicker addTarget:self action:@selector(pickerChange:) forControlEvents:UIControlEventValueChanged];
        datePicker.datePickerMode = UIDatePickerModeDate;
        [self addSubview:datePicker];
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, frame.size.height-50, frame.size.width, 50)];
        [btn setTitle:@"确定" forState:UIControlStateNormal];
        [btn setTitleColor:MAIN_COLOR() forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        UILabel *topLine = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 1)];
        topLine.backgroundColor = RGB(244, 244, 244, 1);
        [self addSubview:topLine];
    }
    return self;
}

-(void)pickerChange:(UIDatePicker *)picker{

    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = _formatter;
    value = [fmt stringFromDate:picker.date];
    
 
}

-(void)submit{
    if (!value) {
        [self pickerChange:datePicker];
    }
    
    if (self.datePickerCallBack) {
        self.datePickerCallBack(value);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
